package cn.edu.zsc.blms.service.impl.array;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.BaseEntity;
import cn.edu.zsc.blms.domain.Book;
import cn.edu.zsc.blms.service.BookService;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import cn.edu.zsc.blms.util.DateUtil;

import java.text.ParseException;
import java.util.Date;

/**
 * Book 业务层的实现类
 */
public class BookServiceImpl extends BaseServiceImpl<BaseEntity> implements BookService {

    /**
     * 存储图书信息
     */
    public final Book[] DATA = new Book[100];

    /**
     * ID计数器
     */
    private int idCounter = 0;

    /**
     * 单例
     */
    private static BookService singleton;

    /**
     * 私有构造方法
     */
    private BookServiceImpl(){

    }

    /**
     * 获取单例，非线程安全
     *
     * @return BookService单例
     */
    public static BookService getSingleton(){
        if(singleton == null){
            singleton =new BookServiceImpl();
        }
        return singleton;
    }

    /**
     * 获取用户输入的有效ID值，返回-1表示用户输入的ID值是无效的
     */

    /**
     * 创建图书
     */

    @Override
    public void create() {
        if (idCounter >= DATA.length) {
            System.out.println("图书数量已满，不能再添加");
            return;
        }
        System.out.print("请输入书名：");
        String name = App.input.nextLine();
        System.out.print("请输入书号：");
        String isbn = App.input.nextLine();
        System.out.print("请输入类目：");
        String category = App.input.nextLine();
        System.out.print("请输入作者：");
        String author = App.input.nextLine();
        System.out.print("请输入出版社：");
        String press = App.input.nextLine();
        System.out.print("请输入单价，请输入正确格式，暂不对做数据格式检测：");
        //TODO:暂不对输入的单价做数据格式检测
        //double price = App.input.nextDouble();

        System.out.print("请输入出版日期，格式为yyyy-MM-dd：");
        Date publishDate = null;
        while (publishDate == null){
            String strDate = App.input.nextLine();
            try{
                publishDate = DateUtil.parse(strDate);
            }catch (ParseException e){
                System.out.print("输入的日期格式错误，请重写输入：");
            }
        }
        App.input.nextLine();
        System.out.print("请输入单价：");
        Double price = null;
        while(price ==null){
            String strPrice = App.input.nextLine();
            try{
                price = Double.parseDouble(strPrice);
            }catch(NumberFormatException e){
                System.out.print("输入的单价格式错误，请重写输入：");
            }
        }
        Book book = new Book(idCounter, name, isbn, category, author, press, publishDate, price);
        DATA[idCounter] = book;
        System.out.println("成功增加1本图书！");
        idCounter++;
    }

    /**
     * 废弃图书（禁用图书）
     */

    @Override
    public void disable() {
        System.out.print("请输入要废弃的图书ID：");
        Book book = null;
        try{
            book = (Book) getOne();
        }catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
        if(book != null){
            book.disabled = true;
            System.out.println("该图书已被废弃");
        }
    }

    /**
     * 编辑图书
     */

    @Override
    public void update() {
        System.out.print("请输入要编辑的图书ID：");
        Book book = null;
        try{
            book = (Book) getOne();
        }catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
        if(book != null){
            System.out.print("请输入书名，直接按回车表示不修改:");
            String name = App.input.nextLine();
            book.name = "".equals(name) ? book.name : name;
            System.out.print("请输入书号，直接按回车表示不修改:");
            String isbn = App.input.nextLine();
            book.isbn = "".equals(isbn) ? book.isbn : name;
            System.out.print("请输入类目，直接按回车表示不修改:");
            String category = App.input.nextLine();
            book.category = equals(category) ? book.category : category;
            System.out.print("请输入作者，直接按回车表示不修改:");
            String author = App.input.nextLine();
            book.author = equals(author) ? book.author : author;
            System.out.print("请输入出版社，直接按回车表示不修改:");
            String press = App.input.nextLine();
            book.press = equals(press) ? book.press : press;
            System.out.print("请输入出版日期，格式为yyyy-MM-dd，直接按回车表示不修改:");
            Date publishDate = null;
            while (publishDate == null) {
                String strDate = App.input.nextLine();
                if ("".equals(strDate)){
                    publishDate = book.publishDate;
                    break;
                }
                try {
                    publishDate = DateUtil.parse(strDate);
                } catch (ParseException e) {
                    System.out.print("输入的日期格式错误，请重写输入:");
                }
            }
            book.publishDate = publishDate;
            System.out.print("请输入单价:");
            Double price = null;
            while (price == null) {
                String strPrice = App.input.nextLine();
                if ("".equals(strPrice)){
                    price = book.price;
                    break;
                }
                try {
                    price = Double.parseDouble(strPrice);
                } catch (NumberFormatException e) {
                    System.out.print("输入的单价格式错误，请重写输入:");
                }
            }
            // TODO: 暂不对输入的单价做数据格式检测
            book.price = App.input.nextDouble();
            App.input.nextLine();
            System.out.println("该图书的信息已被修改");
        }
    }

    //
    /**
     * 查询图书
     */

    @Override
    public void query(){
        System.out.print("请输入要查询的图书名称：");
        String name = App.input.nextLine();
        //TODO:根据图书名称模糊查询，并打印查询结果，暂且打印所有图书信息
        for(int i = 0; i < idCounter; i++){
            Book book =(Book) DATA[i];
            if(book.name != null && book.name.contains(name)){
                System.out.println(book);
            }

        }
    }
    protected BaseEntity getOne() throws EntityNotExistException{
        BaseEntity entity;
        try{
            int id = Integer.parseInt(App.input.nextLine());
            if(id >= idCounter || DATA[id] == null){
                throw new EntityNotExistException();
            }
            entity = DATA[id];
        }catch (NumberFormatException e){
            throw new EntityNotExistException("ID为整数，请输入整数值");
        }
        return entity;
    }
}
